package com.level2app.ui.home;

import android.os.Bundle;
import android.text.TextPaint;
import android.text.style.ClickableSpan;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.level2app.R;
import com.level2app.adapter.CategoriesListAdapter;
import com.level2app.base.BaseFragment;
import com.level2app.databinding.FragmentCategoriesListBinding;
import com.level2app.ui.home.view.CategoriesListFrgViewModel;
import com.level2app.utils.SpanUtils;
import com.level2app.utils.ToastHelper;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by S
 * time 2024/8/17
 */
public class CategoriesListFragment extends BaseFragment<FragmentCategoriesListBinding, CategoriesListFrgViewModel> {

    private int type;
    private CategoriesListAdapter adapter;
    private boolean hasEnoughFans = true;//是否有足够的粉丝

    public static CategoriesListFragment newInstance(int type) {
        CategoriesListFragment categoriesListFragment = new CategoriesListFragment();
        Bundle args = new Bundle();
        args.putInt("type", type);
        categoriesListFragment.setArguments(args);
        return categoriesListFragment;
    }

    @Override
    protected CategoriesListFrgViewModel bindModel() {
        return getViewModel(this, CategoriesListFrgViewModel.class);
    }

    @Override
    protected void initInject() {
        getFragmentComponent().inject(this);
    }

    @Override
    protected void initData() {
        type = getArguments().getInt("type");
        binding.refreshLayout.setRefreshHeader(new ClassicsHeader(getContext()));
        binding.refreshLayout.setRefreshFooter(new ClassicsFooter(getContext()));
        adapter = new CategoriesListAdapter();
        binding.recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        binding.recyclerView.setAdapter(adapter);

        if (type == 1) {
            if (hasEnoughFans) {
                List<String> data = new ArrayList<>();
                data.add("121123");
                data.add("121123");
                data.add("121123");
                data.add("121123");
                data.add("121123");
                adapter.submitList(data);
                binding.noCreatorLl.setVisibility(View.GONE);
                binding.recyclerView.setVisibility(View.VISIBLE);
                binding.refreshLayout.setEnableRefresh(true);
            } else {
                binding.noCreatorLl.setVisibility(View.VISIBLE);
                binding.recyclerView.setVisibility(View.GONE);
                binding.refreshLayout.setEnableRefresh(false);
                handleCreatorText();
            }
        } else {
            binding.recyclerView.setLayoutManager(new GridLayoutManager(getContext(), 1));
            adapter.setEmptyViewLayout(getContext(), R.layout.categories_offer_empty_view);
            adapter.setEmptyViewEnable(true);
            binding.refreshLayout.setEnableRefresh(false);
            binding.refreshLayout.setEnableLoadMore(false);
        }
    }

    @Override
    protected void initClick() {
        super.initClick();
        binding.refreshLayout.setOnRefreshListener(refreshLayout -> {
            //刷新
            binding.refreshLayout.finishRefresh();
            if (hasEnoughFans) {
                binding.refreshLayout.setEnableLoadMore(true);
            }
            //TODO:显示隐藏列表逻辑
//            adapter.submitList(data);
        });
        binding.refreshLayout.setOnLoadMoreListener(refreshLayout -> {
            //加载更多
            binding.refreshLayout.finishLoadMore();
        });
        adapter.setOnItemClickListener((baseQuickAdapter, view, i) -> {

        });
        viewModel.onDelayClick(binding.reactiveBtn, o -> {

        });
    }

    @Override
    protected void lazyLoadData() {
        super.lazyLoadData();
        //TODO:请求写这里

    }

    private void handleCreatorText() {
        String copyWriting = getString(R.string.review_the_creator_mode);
        String spString = "Creator Mode\nPolicy";
        String[] cw = copyWriting.split(spString);
        if (cw.length == 2) {
            SpanUtils.with(binding.contentTv)
                    .append(cw[0])
                    .append(spString)
                    .setClickSpan(new ClickableSpan() {

                        @Override
                        public void onClick(@NonNull View view) {
                            //TODO:跳转
                            ToastHelper.showToast("Creator Mode Policy");
                        }

                        @Override
                        public void updateDrawState(@NonNull TextPaint ds) {
                            super.updateDrawState(ds);
                            ds.setUnderlineText(true);
                            ds.setColor(ContextCompat.getColor(getContext(), R.color.color_141416));
                        }
                    })
                    .append(cw[1])
                    .create();
        }
    }
}